package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;

//	http://127.0.0.1/api/order/orderInfo/trade
	@TsLogin
	@Operation(summary = "订单提交页")
	@PostMapping("/trade")
	public Result trade(@RequestBody TradeVo tradeVo) {
//		从线程中获取用户id
		Long userId = AuthContextHolder.getUserId();
		OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo,userId);
		return Result.ok(orderInfoVo);
	}
//http://127.0.0.1/api/order/orderInfo/submitOrder
	@TsLogin
	@Operation(summary = "订单提交")
	@PostMapping("/submitOrder")
	public Result submitOrder(@RequestBody OrderInfoVo orderInfoVo){
//		从线程中获取userId
		Long userId = AuthContextHolder.getUserId();
		//		调用service层的方法
		String orderNo = orderInfoService.submitOrder(orderInfoVo,userId);
//		封装返回的数据
		Map<String, Object> map = new HashMap<>();
		map.put("orderNo", orderNo);
		return Result.ok(map);
	}
	/**
	 * 查看我的订单
	 * @param orderNo
	 * @return
	 */
	@TsLogin
	@Operation(summary = "根据id获取订单详情")
	@GetMapping("/getOrderInfo/{orderNo}")
	public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
		OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
		return Result.ok(orderInfo);
	}

/**
 * 查看我的订单
 * @param page
 * @param limit
 * @return
 */
	@TsLogin
	@Operation(summary = "获取用户订单")
	@GetMapping("findUserPage/{pageNo}/{pageSize}")
	public Result findUserPage(@PathVariable Long pageNo,
							   @PathVariable Long pageSize) {
//		从线程中获取用户id
		Long userId = AuthContextHolder.getUserId();
//		生成分页对象
		Page<OrderInfo> page = new Page<>(pageNo, pageSize);
//		调用service层的方法
		IPage<OrderInfo> pagelist =  orderInfoService.findUserPage(page,userId);
		return Result.ok(pagelist);
	}
}

